package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityApproveDetail;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityDataApprove;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbCapacityApproveDetailDao {
    @Results(id = "pcbCapacityApproveDetailResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "flow_type", property = "flowType", jdbcType = JdbcType.INTEGER),
            @Result(column = "data_id", property = "dataId", jdbcType = JdbcType.INTEGER),
            @Result(column = "approve_node", property = "approveNode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "apply_user_id", property = "applyUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "next_node_user_id", property = "nextNodeUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "department_id", property = "departmentId", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_id", property = "sonFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "file_ids", property = "fileIds", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "copy_user_id", property = "copyUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "approve_user_id", property = "approveUserId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER)
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select " +
            "id, flow_type, data_id, approve_node, apply_user_id, department_id, son_factory_id, file_ids, remark, copy_user_id, approve_user_id, next_node_user_id, create_user, create_time, update_user, update_time, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL +
            " where id = #{id}" +
            "</script>")
    PcbCapacityApproveDetail getById(@Param("id") Integer id);

    @Select("<script> " +
            "select " +
            "id, flow_type, data_id, approve_node, apply_user_id, department_id, son_factory_id, file_ids, remark, copy_user_id, approve_user_id, next_node_user_id, create_user, create_time, update_user, update_time, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL +
            "<where>" +
            "<if test='flowType != null'>" +
            " and flow_type = #{flowType}" +
            "</if>" +
            "<if test='dataId != null'>" +
            " and data_id = #{dataId}" +
            "</if>" +
            "<if test='approveNode != null'>" +
            " and approve_node = #{approveNode}" +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap(value = "pcbCapacityApproveDetailResultMap")
    List<PcbCapacityApproveDetail> getByCondition(PcbCapacityApproveDetail pcbCapacityApproveDetail);

    @Select("<script> " +
            "select " +
            "id, flow_type, data_id, approve_node, apply_user_id, department_id, son_factory_id, file_ids, remark, copy_user_id, approve_user_id, next_node_user_id, create_user, create_time, update_user, update_time, status, sort " +
            "from " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL +
            " where flow_type = #{flowType} and data_id = #{dataId}" +
            "</script>")
    @ResultMap(value = "pcbCapacityApproveDetailResultMap")
    List<PcbCapacityApproveDetail> getByLimit(@Param("flowType") Integer flowType, @Param("dataId") Integer dataId);

    /*新增所有列*/
    @Insert("insert into " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL +
            " ( flow_type, data_id, approve_node, apply_user_id, department_id, son_factory_id, file_ids, remark, copy_user_id, approve_user_id, next_node_user_id, create_user, create_time, update_user, update_time, status, sort) values " +
            "( #{flowType}, #{dataId}, #{approveNode}, #{applyUserId}, #{departmentId}, #{sonFactoryId}, #{fileIds}, #{remark}, #{copyUserId}, #{approveUserId}, #{nextNodeUserId}, #{createUser}, #{createTime}, #{updateUser}, #{updateTime}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(PcbCapacityApproveDetail param);

    /*更新所有列*/
    @Update("<script>" +
            "update " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL +
            "<set>" +
            "<if test='flowType != null'>" +
            "    flow_type = #{flowType}," +
            "</if>" +
            "<if test='dataId != null'>" +
            "    data_id = #{dataId}," +
            "</if>" +
            "<if test='approveNode != null and approveNode != \"\" '>" +
            "    approve_node = #{approveNode}," +
            "</if>" +
            "<if test='approveUserId != null '>" +
            "    approve_user_id = #{approveUserId}," +
            "</if>" +
            "<if test='departmentId != null '>" +
            "    department_id = #{departmentId}," +
            "</if>" +
            "<if test='sonFactoryId != null '>" +
            "    son_factory_id = #{sonFactoryId}," +
            "</if>" +
            "<if test='fileIds != null and fileIds != \"\" '>" +
            "    file_ids = #{fileIds}," +
            "</if>" +
            "<if test='remark != null and remark != \"\" '>" +
            "    remark = #{remark}," +
            "</if>" +
            "<if test='sort != null'>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='copyUserId != null'>" +
            "    copy_user_id = #{copyUserId}," +
            "</if>" +
            "<if test='approveUserId != null'>" +
            "    approve_user_id = #{approveUserId}," +
            "</if>" +
            "<if test='nextNodeUserId != null'>" +
            "    next_node_user_id = #{nextNodeUserId}," +
            "</if>" +
            "<if test='status != null'>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "    create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    update_time = #{updateTime}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    create_user = #{createUser}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    update_user = #{updateUser}," +
            "</if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    Integer update(PcbCapacityApproveDetail param);

    /*删除*/
    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL + " where id = #{id}")
    Integer deleteById(Integer id);

    @Delete("delete from " + DB2TableName.PCB_CAPAICTY_APPROVE_DETAIL + " where flow_type = #{flowType} and data_id = #{dataId}")
    Integer deleteByFlowType(@Param("flowType") Integer flowType, @Param("dataId") Integer dataId);
}
